import _ from 'underscore'

export default (routes, fullpageRoutes) => {
  // 处理常见路由，路由只存在2级
  routes = routes.map((route) => {
    route = _.pairs(route)[0]
    let key = route[0]
    let value = route[1]
    var component
    if (key.split('/').length === 1) {
      component = () => {
        return Promise.resolve(import(`@/pages/${key.replace(/\/?:[^/]*/g, '')}/index`))
      }
    } else {
      component = () => {
        return Promise.resolve(import(`@/pages/${key.replace(/\/?:[^/]*/g, '')}`))
      }
    }
    return {
      path: `/${key}`,
      component: component,
      children: value.map((childItem) => {
        return {
          path: `${childItem}`,
          component: () => {
            return Promise.resolve(import(`@/pages/${key.replace(/\/?:[^/]*/g, '')}/${childItem.replace(/\/?:[^/]*\/?/g, '')}`))
          }
        }
      })
    }
  })

  // 处理全屏
  routes = routes.concat(fullpageRoutes.map((item) => {
    return {
      path: `/${item}`,
      name: item,
      components: {
        'full-page': () => Promise.resolve(import(`@/pages/${item}/index`))
      }
    }
  }))

  // 处理默认路由
  routes.push({ path: '/', redirect: '/dashboard' })
  // 处理otherwise
  routes.push({ path: '*', redirect: '/404' })
  return routes
}
